package com.dyz.leetcode;

public class ClimbStairs70 {
    //动态规划
    public int climbStairs(int n) {
        if(n<=0){return 0;}
        if(n==1){return 1;}
        //动态规划
        //容器
        int[] dp = new int[n];
        //初始化
        dp[0]=1;
        dp[1]=2;
        for(int i=2; i<n; i++){
            dp[i] = dp[i-1]+dp[i-2];
        }
        return dp[n-1];
    }
    // pre 和prepre变量替换数组，节省空间
    public int climbStairs1(int n) {
        if(n<=0){return 0;}
        if(n==1){return 1;}
        if(n==2){return 2;}
        int pre = 2;
        int prepre =1;
        int cur = 0;
        for(int i=3; i<=n;i++){
            cur = pre+prepre;
            prepre =pre;
            pre = cur;
        }
        return cur;
    }
}
